// 引入
import Vue from 'vue'
import VueRouter from 'vue-router'
// 注册
Vue.use(VueRouter);
// 解决路由传参对promise回调问题的处理-->重写push|replace
let originPush = VueRouter.prototype.push
let originReplace = VueRouter.prototype.replace

VueRouter.prototype.push = function (location, resolve, reject) {
  if (resolve && reject) {
    originPush.call(this, location, resolve, reject);
  } else {
    originPush.call(this, location, () => { }, () => { })
  }
}
VueRouter.prototype.replace = function (location, resolve, reject) {
  if (resolve && reject) {
    originReplace.call(this, location, resolve, reject);
  } else {
    originReplace.call(this, location, () => { }, () => { })
  }
}

// 注册实例
let router = new VueRouter({
  // meta路由参数用于确定是否显示头部导航
  routes: [
    {
      path: '/',
      component: () => import('@/views/home/index.vue'),
      meta: { show: true }
    },
    {
      path: '/inform',
      component: () => import('@/views/inform/index.vue'),
      meta: { show: true }
    },
    {
      path: '/publish',
      component: () => import('@/views/publish/index.vue'),
      meta: { show: true }
    },
    {
      path: '/confessionWall',
      component: () => import('@/views/confessionWall/index.vue'),
      meta: { show: true }
    },
    {
      path: '/wish',
      component: () => import('@/views/wish/index.vue'),
      meta: { show: true }
    },
    {
      path: '/blessing',
      component: () => import('@/views/blessing/index.vue'),
      meta: { show: true }
    },
    {
      name: 'letter',
      path: '/letter/:uuId',
      component: () => import('@/views/letter/index.vue'),
    },
    {
      name: '/mine',
      path: '/mine/:uuId',
      component: () => import('@/views/mine/index.vue'),
      meta: { show: true }
    },
    {
      path: '/memo',
      component: () => import('@/views/memo/index.vue'),
    },
    {
      name: '/login',
      path: '/login',
      component: () => import('@/views/login/index.vue'),
      meta: { show: true }
    },
    {
      path: '/register',
      component: () => import('@/views/register/index.vue'),
      meta: { show: true }
    },
    {
      name: '/search',
      path: '/search',
      component: () => import('@/views/search/index.vue'),
      meta: { show: true }
    },
    {
      // 路由重定项
      path: '*',
      redirect: '/'
    }
  ],
  // 滚动行为的配置
  scrollBehavior(to, from, savedPosition) {
    // return 期望滚动到哪个的位置
    return { y: 0 }
  }
})

// 使用前置路由，来进行路由跳转前的判断
router.beforeEach((to, from, next) => {
  /* 问题: 1.登录之后是不能跳转到登录和注册页面
  ** 2.未登录只能访问首页，登录，注册,书信,我的页面
  **   在前置路由配置函数里是没有this指向的，为undefined
  */
  let token = localStorage.getItem('WALLTOKEN')
  // 如果有token
  if (token) {
    // 再判断将要跳转页面
    if (to.path === "/login" || to.path === "/register") {
      next('/')
    }
    // 必须要这个放行函数
    next()
  } else if (!token) {
    if (to.path !== "/" && to.path !== "/register" && to.path !== '/login') {
      // 再判断是不是书信
      if (to.path.indexOf('/letter') !== -1) {
        // 是则放行
        next()
      } else {
        // 跳转到登录页面
        next('/login')
      }
    }
    next()
  }
})

// 默认导出
export default router